﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using Entities;
using System.Data;

namespace DAL
{
    public class SanPhamDAL
    {
        public static List<SanPham> LayTatCa() // lấy tất cả dữ liệu trong bảng Sản Phẩm
        {
            try
            {
                return CBO.FillCollection<SanPham>(DataProvider.Instance.ExecuteReader("SanPham_LayTatCa"));
            }
            catch (Exception)
            {
                return null;
            }
        }
        public static SanPham TimDon(string masp) // Tìm đơn, tìm 1 sản phẩm
        {
            try
            {
                return CBO.FillObject<SanPham>(DataProvider.Instance.ExecuteReader("SanPham_TimDon", Convert.ToInt32(masp)));
            }
            catch (Exception)
            {
                return null;
            }
        }
        public static bool Them(SanPham sp)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("SanPham_Them", sp.IDSP, sp.TenSP, sp.Gia, sp.DonVi, sp.NgayTao, sp.NgayGui, sp.HinhAnh, sp.AnhXemTruoc, sp.MoTa, sp.IDChuDe, sp.IDLoaiSP, sp.HienThi);
                return Convert.ToInt32(rs) > 0;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Xoa(string masp)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("SanPham_Xoa", Convert.ToInt32(masp));
                return Convert.ToInt32(rs) > 0;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Sua(SanPham sp)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("SanPham_Sua", sp.IDSP, sp.TenSP, sp.Gia, sp.DonVi, sp.NgayTao, sp.NgayGui, sp.HinhAnh, sp.AnhXemTruoc, sp.MoTa, sp.IDChuDe, sp.IDLoaiSP, sp.HienThi);
                return Convert.ToInt32(rs) > 0;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static List<SanPham> TrongChuDe(string machude, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader(
                    "SanPham_TrongChuDe", Convert.ToInt32(machude), Convert.ToInt32(page), pageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<SanPham>(reader);
            }
            catch (Exception)
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<SanPham>();
            }
        }
        public static List<SanPham> SanPham_HienThi(string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("SanPham_HienThi", Convert.ToInt32(page), pageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<SanPham>(reader);
            }
            catch (Exception)
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<SanPham>();
            }
        }
        public static List<SanPham> TimTheoTen(string ten)
        {
            try
            {
                return CBO.FillCollection<SanPham>(DataProvider.Instance.ExecuteReader("SanPham_TimKiemTenSP", ten));
            }
            catch
            {

                return new List<SanPham>();
            }
        }
        public static List<SanPham> TimTheoTenChuDe(string chude)
        {
            try
            {
                return CBO.FillCollection<SanPham>(DataProvider.Instance.ExecuteReader("SanPham_TimChuDe", chude));
            }
            catch
            {

                return new List<SanPham>();
            }
        }
        public static List<SanPham> TimTheoTenLoaiSP(string loaisp)
        {
            try
            {
                return CBO.FillCollection<SanPham>(DataProvider.Instance.ExecuteReader("SanPham_TimLoaiSP", loaisp));
            }
            catch
            {

                return new List<SanPham>();
            }
        }
        public static List<SanPham> TimTheoGia(double gia1, double gia2)
        {
            try
            {
                return CBO.FillCollection<SanPham>(DataProvider.Instance.ExecuteReader("SanPham_TimKiemGia", Convert.ToDouble(gia1), Convert.ToDouble(gia2)));
            }
            catch
            {

                return new List<SanPham>();
            }
        }
        public static List<SanPham> TimKiem(string ten, double gia1, double gia2)
        {
            try
            {
                return CBO.FillCollection<SanPham>(DataProvider.Instance.ExecuteReader("SanPham_TimKiem", ten, Convert.ToDouble(gia1), Convert.ToDouble(gia1)));
            }
            catch
            {

                return new List<SanPham>();
            }
        }
        public static bool CapNhatHienThi(string Id, bool hienthi)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteScalar("SanPham_CapNhatHienThi", Id, hienthi);
                return Convert.ToInt32(rs) > 0;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
